<template>
  <div style="padding: 20px;">
    <el-row :gutter="gutter">
      <el-col :span="12">
        <el-card style="height: 680px;" shadow="never">
          <div slot="header">
            <span>取货地</span>
          </div>
          <el-form label-position="top" :model="formData.shipperAddress" size="small">
            <el-row :gutter="gutter">
              <el-col :span="12">
                <el-form-item>
                  <slot name="label">
                    地址类型
                  </slot>
                  <el-select v-model.trim="formData.shipperAddress.type" filterable style="width:100%">
                    <el-option v-for="(item) in addressType" :key="item.value" :label="item.name" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item>
                  <slot name="label">
                    增值服务
                    <el-tooltip effect="light" content="Inside: 用于需要交付至拖车以外区域的装运。必须有电梯为拖车上方或下方的楼层提供服务。Lif-Gate: 在装货码头不可用时选择此服务。提升门是一些卡车后面的机械平台上用" placement="top">
                      <i class="el-icon-question" />
                    </el-tooltip>
                  </slot>
                  <el-select v-model.trim="formData.shipperAddress.additionalService" filterable style="width:100%">
                    <el-option v-for="(item) in additionalService" :key="item.value" :label="item.name" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="gutter">
              <el-col :span="12">
                <el-form-item label="取货码">
                  <el-input v-model="formData.shipperAddress.code" auto-complete="off" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="客户订单号">
                  <el-input v-model="formData.customerRef" auto-complete="off" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="gutter">
              <el-col :span="8">
                <el-form-item label="联系人" required>
                  <el-input v-model="formData.shipperAddress.contacter" auto-complete="off" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="联系电话" required>
                  <el-input v-model.trim="formData.shipperAddress.phone" auto-complete="off" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="联系邮箱" required>
                  <el-input v-model.trim="formData.shipperAddress.email" auto-complete="off" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="gutter">
              <el-col :span="8">
                <el-form-item label="国家" required>
                  <el-select v-model="formData.shipperAddress.countryCode" filterable style="width:100%">
                    <el-option v-for="(item,i) in countrys" :key="i" :label="item.nameCn" :value="item.code" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="州/省" required>
                  <el-select v-model="formData.shipperAddress.state" filterable style="width:100%">
                    <el-option v-for="(item,i) in states" :key="i" :label="item.name" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="城市" required>
                  <el-input v-model.trim="formData.shipperAddress.city" auto-complete="off" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="gutter">
              <el-col :span="8">
                <el-form-item label="地址1" required>
                  <el-input v-model="formData.shipperAddress.address" auto-complete="off" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="地址2">
                  <el-input v-model.trim="formData.shipperAddress.address2" auto-complete="off" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="邮编" required>
                  <el-input v-model.trim="formData.shipperAddress.zipCode" auto-complete="off" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="gutter">
              <el-col :span="8">
                <el-form-item label="取货日期" required>
                  <el-date-picker
                    v-model="formData.shipperAddress.at"
                    type="date"
                    value-format="yyyy-MM-dd"
                    style="width:100%"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="起始时间" required>
                  <el-time-select
                    v-model="formData.shipperAddress.startTime"
                    value-format="HH:mm"
                    :picker-options="{
                      start: '08:30',
                      step: '00:15',
                      end: '18:00'
                    }"
                    style="width:100%"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="结束时间" required>
                  <el-time-select
                    v-model="formData.shipperAddress.endTime"
                    value-format="HH:mm"
                    :picker-options="{
                      start: '08:30',
                      step: '00:15',
                      end: '18:30'
                    }"
                    style="width:100%"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="gutter">
              <el-col :span="24">
                <el-form-item label="备注">
                  <el-input v-model.trim="formData.shipperAddress.remark" type="textarea" :rows="2" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card style="height: 680px;" shadow="never">
          <div slot="header">
            <span>收货地</span>
          </div>
          <div>
            <el-form label-position="top" :model="formData.shipperAddress" size="small">
              <el-row :gutter="gutter">
                <el-col :span="12">
                  <el-form-item>
                    <slot name="label">
                      地址类型
                    </slot>
                    <el-select v-model.trim="formData.consigneeAddress.type" filterable style="width:100%">
                      <el-option v-for="(item) in addressType" :key="item.value" :label="item.name" :value="item.value" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item>
                    <slot name="label">
                      增值服务
                      <el-tooltip effect="light" content="Inside: 用于需要交付至拖车以外区域的装运。必须有电梯为拖车上方或下方的楼层提供服务。Lif-Gate: 在装货码头不可用时选择此服务。提升门是一些卡车后面的机械平台上用" placement="top">
                        <i class="el-icon-question" />
                      </el-tooltip>
                    </slot>
                    <el-select v-model.trim="formData.consigneeAddress.additionalService" filterable style="width:100%">
                      <el-option v-for="(item) in additionalService" :key="item.value" :label="item.name" :value="item.value" />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="gutter">
                <el-col :span="8">
                  <el-form-item label="联系人" required>
                    <el-input v-model="formData.consigneeAddress.contacter" auto-complete="off" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="联系电话" required>
                    <el-input v-model.trim="formData.consigneeAddress.phone" auto-complete="off" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="联系邮箱" required>
                    <el-input v-model.trim="formData.consigneeAddress.email" auto-complete="off" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="gutter">
                <el-col :span="8">
                  <el-form-item label="国家" required>
                    <el-select v-model="formData.consigneeAddress.countryCode" filterable style="width:100%">
                      <el-option v-for="(item,i) in countrys" :key="i" :label="item.nameCn" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="州/省" required>
                    <el-select v-model="formData.consigneeAddress.state" filterable style="width:100%">
                      <el-option v-for="(item,i) in states" :key="i" :label="item.name" :value="item.value" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="城市" required>
                    <el-input v-model.trim="formData.consigneeAddress.city" auto-complete="off" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="gutter">
                <el-col :span="8">
                  <el-form-item label="地址1" required>
                    <el-input v-model="formData.consigneeAddress.address" auto-complete="off" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="地址2">
                    <el-input v-model.trim="formData.consigneeAddress.address2" auto-complete="off" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="邮编" required>
                    <el-input v-model.trim="formData.consigneeAddress.zipCode" auto-complete="off" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="gutter">
                <el-col :span="8">
                  <el-form-item label="交货日期" required>
                    <el-date-picker
                      v-model="formData.consigneeAddress.at"
                      type="date"
                      value-format="yyyy-MM-dd"
                      style="width:100%"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="起始时间" required>
                    <el-time-select
                      v-model="formData.consigneeAddress.startTime"
                      value-format="HH:mm"
                      :picker-options="{
                        start: '08:30',
                        step: '00:15',
                        end: '18:00'
                      }"
                      style="width:100%"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="结束时间" required>
                    <el-time-select
                      v-model="formData.consigneeAddress.endTime"
                      value-format="HH:mm"
                      :picker-options="{
                        start: '08:30',
                        step: '00:15',
                        end: '18:30'
                      }"
                      style="width:100%"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="gutter">
                <el-col :span="24">
                  <el-form-item label="备注">
                    <el-input v-model.trim="formData.consigneeAddress.remark" type="textarea" :rows="2" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-card v-for="(item,index) in formData.cartons" :key="index" style="margin-top: 20px;" shadow="never">
      <div slot="header">
        <span>托{{ index + 1 }}</span>
        <div style="float: right;">
          <el-button style="padding: 3px 0" type="text" @click="add(index)">新增</el-button>
          <el-button style="padding: 3px 0" type="text" @click="copy(index)">复制</el-button>
          <el-button style="padding: 3px 0" type="text" @click="del(index)">删除</el-button>
        </div>
      </div>
      <el-form label-position="top" size="small">
        <el-row :gutter="gutter">
          <el-col :span="20" style="border-radius: 8px;background-color: #f5f5f5;">
            <el-row type="flex" justify="space-between">
              <el-col :span="4">
                <el-form-item label="商品名称">
                  <el-input v-model.trim="item.skuNameCn" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label="货值">
                  <el-input v-model.trim="item.unitPrice" type="number">
                    <template slot="prepend">$</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <slot name="label">
                    NMFC
                    <el-tooltip effect="light" content="The NMFC ltem number, of the commodity lne item" placement="top">
                      <i class="el-icon-question" />
                    </el-tooltip>
                  </slot>
                  <el-input v-model.trim="item.nmfc" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <slot name="label">
                    NMFC Sub
                    <el-tooltip effect="light" content="The NMFC Sub ltem number, if any, of the commodity lne item" placement="top">
                      <i class="el-icon-question" />
                    </el-tooltip>
                  </slot>
                  <el-input v-model.trim="item.nmfcSub" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label="拖是否可堆叠">
                  <el-select v-model.trim="item.stackable" style="width:100%">
                    <el-option label="Stackable" value="true" />
                    <el-option label="Non Stackable" value="false" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row type="flex" justify="space-between">
              <el-col :span="4">
                <el-form-item label="托长(IN)">
                  <el-input v-model.trim="item.length" type="number" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label="托宽(IN)">
                  <el-input v-model.trim="item.width" type="number" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label="托高(IN)">
                  <el-input v-model.trim="item.height" type="number" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label="托重(LB)">
                  <el-input v-model.trim="item.weight" type="number" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label="密度">
                  <el-select v-model.trim="item.freightClass" filterable style="width:100%">
                    <el-option v-for="(item2) in freightClass" :key="item2.value" :label="item2.name" :value="item2.value" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="4">
            <el-row>
              <el-col :span="24">
                <el-form-item label="托数量">
                  <el-input v-model.trim="item.sameSpecCartons" type="number" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="托类型">
                  <el-select v-model.trim="item.type" filterable style="width:100%">
                    <el-option label="PALLETS" value="PT1" />
                    <el-option label="CRATES" value="PT2" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <div style="display:flex;justify-content:center;margin-top: 10px;">
      <el-button size="small" type="primary" @click="rate()">试算运费</el-button>
      <el-button size="small" type="primary" @click="submit()">确 定</el-button>
    </div>
  </div>
</template>
<script>
import request from '@/utils/request'
import { mapState } from 'vuex'
import { copyProperty } from '@/utils/tools'
export default {
  data() {
    return {
      activeNames: '1',
      gutter: 20,
      addressType: [
        { value: 'AT0', name: 'Business with Dock' },
        { value: 'AT1', name: 'Business with Dock' },
        { value: 'AT2', name: 'Business with Dock' }
      ],
      additionalService: [
        { value: 'AS1', name: 'Inside' },
        { value: 'AS2', name: 'Lift-Gate' },
        { value: 'AS3', name: 'APPT' },
        { value: 'AS4', name: 'ROC' },
        { value: 'AS5', name: 'WGD' },
        { value: 'AS6', name: 'WWA' }
      ],
      freightClass: [
        { value: '50', name: '50' },
        { value: '55', name: '55' },
        { value: '60', name: '60' },
        { value: '65', name: '65' },
        { value: '70', name: '70' },
        { value: '77.5', name: '77.5' },
        { value: '85', name: '85' },
        { value: '92.5', name: '92.5' },
        { value: '100', name: '100' },
        { value: '110', name: '110' },
        { value: '125', name: '125' },
        { value: '150', name: '150' },
        { value: '175', name: '175' },
        { value: '200', name: '200' },
        { value: '250', name: '250' },
        { value: '300', name: '300' },
        { value: '400', name: '400' },
        { value: '500', name: '500' }
      ],
      states: [
        { value: 'AL', name: '阿拉巴马州' },
        { value: 'AK', name: '阿拉斯加州' },
        { value: 'AZ', name: '亚利桑那州' },
        { value: 'AR', name: '阿肯色州' },
        { value: 'CA', name: '加利福尼亚州' },
        { value: 'CO', name: '科罗拉多州' },
        { value: 'CT', name: '康涅狄格州' },
        { value: 'DE', name: '特拉华州' },
        { value: 'FL', name: '佛罗里达州' },
        { value: 'GA', name: '乔治亚州' },
        { value: 'HI', name: '夏威夷州' },
        { value: 'ID', name: '爱达荷州' },
        { value: 'IL', name: '伊利诺伊州' },
        { value: 'IN', name: '印地安纳州' },
        { value: 'IA', name: '艾奥瓦州' },
        { value: 'KS', name: '堪萨斯州' },
        { value: 'KY', name: '肯塔基州' },
        { value: 'LA', name: '路易斯安那州' },
        { value: 'ME', name: '缅因州' },
        { value: 'MD', name: '马里兰州' },
        { value: 'MA', name: '马萨诸塞州' },
        { value: 'MI', name: '密歇根州' },
        { value: 'MN', name: '明尼苏达州' },
        { value: 'MS', name: '密西西比州' },
        { value: 'MO', name: '密苏里州' },
        { value: 'MT', name: '蒙大拿州' },
        { value: 'NE', name: '内布拉斯加州' },
        { value: 'NV', name: '内华达州' },
        { value: 'NH', name: '新罕布什尔州' },
        { value: 'NJ', name: '新泽西州' },
        { value: 'NM', name: '新墨西哥州' },
        { value: 'NY', name: '纽约州' },
        { value: 'NC', name: '北卡罗来纳州' },
        { value: 'ND', name: '北达科他州' },
        { value: 'OH', name: '俄亥俄州' },
        { value: 'OK', name: '俄克拉荷马州' },
        { value: 'OR', name: '俄勒冈州' },
        { value: 'PA', name: '宾夕法尼亚州' },
        { value: 'RI', name: '罗德岛州' },
        { value: 'SC', name: '南卡罗来纳州' },
        { value: 'SD', name: '南达科他州' },
        { value: 'TN', name: '田纳西州' },
        { value: 'TX', name: '德克萨斯州' },
        { value: 'UT', name: '犹他州' },
        { value: 'VT', name: '佛蒙特州' },
        { value: 'VA', name: '弗吉尼亚州' },
        { value: 'WA', name: '华盛顿州' },
        { value: 'WV', name: '西弗吉尼亚州' },
        { value: 'WI', name: '威斯康星州' },
        { value: 'WY', name: '怀俄明州' }
      ],
      formDataDefault: {
        channelId: '',
        ref: '',
        shipperAddress: {
          contacter: '',
          phone: '',
          countryCode: 'US',
          state: '',
          city: '',
          address: '',
          address2: '',
          company: '',
          zipCode: '',
          fbaCode: '',
          deliverySignature: 'false',
          type: '',
          additionalService: '',
          code: '',
          at: '',
          startTime: '',
          endTime: '',
          email: '',
          remark: ''
        },
        consigneeAddress: {
          contacter: '',
          phone: '',
          countryCode: 'US',
          state: '',
          city: '',
          address: '',
          address2: '',
          company: '',
          zipCode: '',
          fbaCode: '',
          deliverySignature: 'false',
          type: '',
          additionalService: '',
          code: '',
          at: '',
          startTime: '',
          endTime: '',
          email: '',
          remark: ''
        },
        cartons: []
      },
      formData: {
        channelId: '0',
        customerRef: '',
        shipperAddress: {
          contacter: '',
          phone: '',
          countryCode: 'US',
          state: '',
          city: '',
          address: '',
          address2: '',
          company: '',
          zipCode: '',
          fbaCode: '',
          deliverySignature: 'false',
          type: '',
          additionalService: '',
          code: '',
          at: '',
          startTime: '',
          endTime: '',
          email: '',
          remark: ''
        },
        consigneeAddress: {
          contacter: '',
          phone: '',
          countryCode: 'US',
          state: '',
          city: '',
          address: '',
          address2: '',
          company: '',
          zipCode: '',
          fbaCode: '',
          deliverySignature: 'false',
          type: '',
          additionalService: '',
          code: '',
          at: '',
          startTime: '',
          endTime: '',
          email: '',
          remark: ''
        },
        cartons: [
          { skuNameCn: '', unitPrice: '', stackable: '', nmfc: '', nmfcSub: '', length: '', width: '', height: '', weight: '', freightClass: '', sameSpecCartons: 1, type: '' }
        ]
      }
    }
  },
  computed: {
    ...mapState(
      {
        countrys: state => state.base.countrys
      })
  },
  created() {
    if (this.countrys.length === 0) {
      this.$store.dispatch('getCountrys')
    }
  },
  methods: {
    rate() {

    },
    submit() {
      if (!this.formData.shipperAddress.type) {
        this.$message.error('取货地 地址类型 必填')
        return
      }
      if (!this.formData.consigneeAddress.type) {
        this.$message.error('收货地 地址类型 必填')
        return
      }
      if (!this.formData.shipperAddress.additionalService) {
        this.formData.shipperAddress.additionalService = null
      }
      if (!this.formData.consigneeAddress.additionalService) {
        this.formData.consigneeAddress.additionalService = null
      }
      for (let i = 0; i < this.formData.cartons.length; i++) {
        const e = this.formData.cartons[i]
        if (!e.type) {
          this.$message.error('托' + (i + 1) + ' 托类型 必填')
          return
        }
      }
      request({
        url: '/truck2',
        method: 'post',
        data: this.formData
      }).then(res => {
        this.$message.success('成功')
        copyProperty(this.formData, this.formDataDefault)
      })
    },
    add() {
      this.formData.cartons.push({ skuNameCn: '', unitPrice: '', stackable: '', nmfc: '', nmfcSub: '', length: '', width: '', height: '', weight: '', freightClass: '', sameSpecCartons: 1, type: '' })
    },
    copy(index) {
      const p = { skuNameCn: '', unitPrice: '', stackable: '', nmfc: '', nmfcSub: '', length: '', width: '', height: '', weight: '', freightClass: '', sameSpecCartons: 1, type: '' }
      copyProperty(p, this.formData.cartons[index])
      this.formData.cartons.push(p)
    },
    del(index) {
      this.formData.cartons.splice(index, 1)
    }
  }
}
</script>
<style>
.el-form--label-top .el-form-item__label {
    padding: 0 0 0px;
}
.el-card__header{
  padding: 10px 0px 10px 10px;
  background-color: #f5f5f5;
  font-weight: 700;
  font-size: 16px;
}
.el-card{
  border: 1px dashed #d8d8d8;
}
</style>
